﻿using System.Threading.Tasks;

namespace Sidvall.Web.IO
{
    public class Store
    {
        #region Public Members

        public string StoreId { get; set; }
        public string RootPath { get; set; }
        public string AuthorizationManagerFactory { get; set; }
        public string AuthorizationManagerFactoryContext { get; set; }
        public string FileManagerStrategy { get; set; }
        public string FileManagerStrategyContext { get; set; }
        public string StorageManagerFactory { get; set; }
        public string StorageManagerFactoryContext { get; set; }

        #region GetAuthorizationManager

        private Sidvall.Security.AuthorizationManager _AuthorizationManager;
        public Sidvall.Security.AuthorizationManager GetAuthorizationManager()
        {
            if (_AuthorizationManager != null)
                return _AuthorizationManager;
            return Sidvall.Web.IO.SystemContext.Current.AuthorizationManager;
        }

        #endregion
        #region SetAuthorizationManager

        public void SetAuthorizationManager(Sidvall.Security.AuthorizationManager manager)
        {
            _AuthorizationManager = manager;
        }

        #endregion
        #region GetFileManagerStrategy

        private Sidvall.Web.IO.IFileManagerStrategy _FileManagerStrategy;
        public Sidvall.Web.IO.IFileManagerStrategy GetFileManagerStrategy()
        {
            return _FileManagerStrategy;
        }

        #endregion
        #region SetFileManagerStrategy

        public void SetFileManagerStrategy(Sidvall.Web.IO.IFileManagerStrategy manager)
        {
            _FileManagerStrategy = manager;
        }

        #endregion
        #region GetStorageManager

        private Sidvall.IO.StorageManager _StorageManager;
        public Sidvall.IO.StorageManager GetStorageManager()
        {
            return _StorageManager;
        }

        #endregion
        #region SetStorageManager

        public void SetStorageManager(Sidvall.IO.StorageManager manager)
        {
            _StorageManager = manager;
        }

        #endregion
        #region InitializeAsync

        public async Task InitializeAsync()
        {
            _AuthorizationManager = await Sidvall.Security.SecurityManager.CreateAuthorizationManagerAsync(this.AuthorizationManagerFactory,
                this.AuthorizationManagerFactoryContext).ConfigureAwait(false);
            _FileManagerStrategy = await CreateStorageManagerAsync(this.FileManagerStrategy, this.FileManagerStrategyContext).ConfigureAwait(false);
            _StorageManager = await Sidvall.IO.StorageGroupManager.CreateStorageManagerAsync(this.StorageManagerFactory,
                this.StorageManagerFactoryContext).ConfigureAwait(false);
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return this.RootPath;
        }

        #endregion

        #endregion
        #region Private Members

        #region CreateStorageManagerAsync

        private static async Task<Sidvall.Web.IO.IFileManagerStrategy> CreateStorageManagerAsync(string factoryTypeName, string context)
        {
            var item = new Sidvall.Reflection.InstanceItem()
            {
                FactoryTypeName = factoryTypeName,
                Context = context,
            };
            return await Sidvall.SystemContext.Current.CodeManager.CreateInstanceAsync<Sidvall.Web.IO.IFileManagerStrategy>(item).ConfigureAwait(false);
        }

        #endregion

        #endregion
    }
}
